<?php
/*
 * @Author: your name
 * @Date: 2020-12-25 17:21:31
 * @LastEditTime: 2021-01-05 10:11:36
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /special/application/adminapiv3/controller/module_manage/NetfilterBaseController.php
 */
namespace app\adminapiv3\controller\module_manage\netfilter;

use app\adminapiv3\controller\BaseController;
use app\common\definition\netfilter\NetfilterConfigDefinition;
use app\common\exception\CustomException;
use think\Db;

class NetfilterBaseController extends BaseController
{
    //前置执行方法
    protected $beforeActionList = [
        //检查是正在学习
        'checkLearn' => [
            'only' =>
            /*================= netfilter 模块 =================*/
            //修改 linux/windows 防火墙默认行为, 
            'linuxFirewallConfigModify,windowsFirewallConfigModify,' . 
            //接管/放弃接管
            'controlledFirewall,uncontrolledFirewall,' .
            //启用/禁用 通信规则
            'safeRuleOn,safeRuleOff,' .
            //添加/修改/删除/启用/禁用 手动规则
            'firewallRuleAdd,firewallRuleModify,firewallRuleDelete,firewallRuleOn,firewallRuleOff,' .
            //删除/启用/禁用 原始规则
            'originRuleDelete,on,off,' .
            //学习配置修改 关闭学习 批量开启学习 单个开启学习 删除学习数据 创建自学习防火墙规则
            'configModify,bulkLearnOn,oneLearnOn,learnDataDelete,createFirewallRule,' .
            //开启日志记录 关闭日志记录, 修改日志记录选项
            'open,close,modifyConfig,' .
            //回滚规则 排序规则
            'reset,sort,pushToClient'
        ],
        //检测是否接管了防火墙
        'checkControlled' => [
            'only' => 
            //修改 linux/windows 防火墙默认行为, 
            'linuxFirewallConfigModify,windowsFirewallConfigModify,' . 
            //放弃接管防火墙
            'uncontrolledFirewall,' .
            //启用/禁用 通信规则
            'safeRuleOn,safeRuleOff,' .
            //添加/修改/删除/启用/禁用 手动规则
            'firewallRuleAdd,firewallRuleModify,firewallRuleDelete,firewallRuleOn,firewallRuleOff,' .
            //删除/启用/禁用 原始规则
            'originRuleDelete,on,off,' .
            //学习配置修改 删除学习数据 创建自学习防火墙规则
            'learnDataDelete,createFirewallRule,' .
            //开启日志记录 关闭日志记录, 修改日志记录选项
            'open,close,modifyConfig,' .
            //回滚规则 排序规则
            'reset,sort,pushToClient'
        ]
    ];
    protected $host_netfilter_configs = [];

    function initialize ()
    {
        parent::initialize();
        $host_id_a = $this->getHost('id');
        $this->host_netfilter_configs = Db::name('host_learn_config')->field('host_id,controlled_firewall,learn_status')->whereIn('host_id', $host_id_a)->select();
    }

    /**
     * @name: 检测主机学习状态
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-12-25 16:37:20
     */
    protected function checkLearn ()
    {
        $leaning_host = [];
        foreach ($this->host_netfilter_configs as $host_a)
        {
            if ($host_a['learn_status'] == NetfilterConfigDefinition::LEARNING)
            {
                $leaning_host[] = $host_a['host_id'];
            }
        }

        if ($leaning_host)
        {
           throw new CustomException($this->hostErrorMessage($leaning_host, '主机处于学习中,不能修改防火墙配置'), 422);
        }
    }

    /**
     * @name: 检测是否接管防火墙
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-12-28 13:46:01
     */
    protected function checkControlled ()
    {
        $controlled_host = [];
        foreach ($this->host_netfilter_configs as $host_a)
        {
            if ($host_a['controlled_firewall'] == NetfilterConfigDefinition::FIREWALL_NOT_CONTROLLED)
            {
                $controlled_host[] = $host_a['host_id'];
            }
        }

        if ($controlled_host)
        {
           throw new CustomException($this->hostErrorMessage($controlled_host, '主机未能接管防火墙,不能进行操作'), 422);
        }
    }
}